import { getStorage } from "./auth";

export function getWxLoginCode(){
	return new Promise((resolve,reject)=>{
		wx.login({
			timeout:10*1000,
			success: (res) => {
				if(res.code){
					resolve(res.code);
				}else{
					uni.showToast({
						title:'获取登录信息失败',
						mask:true
					})
					reject()
				}
			},
			fail:()=>{
				uni.showToast({
					title:'获取登录信息失败',
					mask:true
				})
				reject()
			}
		})
	})
}
export function checkLogin(back=true){
	const hasToken = getStorage('token');
	const user = getStorage('user');
	// #ifdef MP-WEIXIN
	const condition=user&&user.mobile&&hasToken;
	// #endif
	// #ifdef H5
	const condition=hasToken;
	// #endif
	if(!condition){
		// #ifdef MP-WEIXIN
		uni.showModal({
			title: '提示',
			content: '需要先登录！',
			cancelText: '先逛逛',
			confirmText: '去登录',
			success(res) {
				if (res.confirm) {
					uni.switchTab({
						url: '/pages/mine/index'
					})
				}else{
					if(back){
						uni.switchTab({
							url:'/pages/index/index'
						})
					}
					
				}
			}
		})
		// #endif
		// #ifdef H5
		uni.redirectTo({
			url:'/pages_pack/h5/login'
		})
		// #endif
		return false;
	}
	return true;
}